let token;

function call(option) {
	return new Promise((resolve, reject) => {
		if (!option.data) option.data = {}
		if (token) option.data.token = token
		uni.showLoading({
			"title": "加载中..."
		})
		uniCloud.callFunction({
			name: option.name,
			data: option.data,
			success: (res) => {
				if (res.result.data && res.result.data.token) token = res.result.data.token
				if (res && res.result && res.result.code !== 200) {
					uni.showToast({
						icon: "none",
						title: res.result.message
					})
					if (option.fail) option.fail(res.result)
					reject(res.result)
				} else {
					if (option.success) option.success(res)
					resolve(res)
				}
			},
			fail: (err) => {
				uni.showToast({
					icon: "none",
					title: "请求失败"
				})
				if (option.fail) option.fail(err)
				reject(err)
			},
			complete: () => {
				uni.hideLoading()
			}
		})
	})
}

module.exports = {
	call: call
}
